package so.shanku.essential.getdata;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import net.tsz.afinal.json.JsonMapOrListJsonMap2JsonUtil;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import so.shanku.essential.utils.LogUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/12/3 0003.
 * <p/>
 * 网络请求队列
 */
public class GetServicesDataUtil {
    private String TAG = "GetDataQueue";
    private HttpUtils httpUtils;
    private static GetServicesDataUtil getServicesDataUtil;

    public static GetServicesDataUtil init() {
        if (getServicesDataUtil == null) {
            getServicesDataUtil = new GetServicesDataUtil();
        }

        return getServicesDataUtil;
    }

    private GetServicesDataUtil() {
        httpUtils = new HttpUtils();
    }

    public void getData(final GetDataQueue dataRequest) {
        Map jsonMap = dataRequest.getParams();
//        StringRequest stringRequest = new StringRequest(Request.Method.POST, dataRequest.getIp() + dataRequest.getActionName(),  new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                dataRequest.setNetError(false);
//                            LogUtil.d("resp data:", response);
//                            dataRequest.setResponseData(response);
//                            dataRequest.getCallBack().onLoaded(
//                                    dataRequest);
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        }){
//            @Override
//            protected Map<String, String> getParams() throws AuthFailureError {
//                return dataRequest.getParams();
//            }
//        };

        JsonMapOrListJsonMap2JsonUtil myJsonMapOrList2JsonStrUtils = new JsonMapOrListJsonMap2JsonUtil();
        String requestBody = myJsonMapOrList2JsonStrUtils.map2Json(jsonMap);
        LogUtil.d("req data:", requestBody);

        try {
            RequestParams params = new RequestParams();
            //StringEntity stringEntity = new StringEntity(requestBody, HTTP.UTF_8);
            ArrayList postData = new ArrayList();
            if(jsonMap != null) {
                Iterator response = jsonMap.entrySet().iterator();

                while(response.hasNext()) {
                    Map.Entry entity = (Map.Entry)response.next();
                    postData.add(new BasicNameValuePair((String)entity.getKey(), (String)entity.getValue()));
                }
            }

            UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(postData,HTTP.UTF_8);

            params.setBodyEntity(entity1);
//            Set<Map.Entry> entry = jsonMap.entrySet();
//
//            for (Map.Entry jsonMap1 : entry) {
//                params.addQueryStringParameter("" + jsonMap1.getKey(), ""
//                        + jsonMap1.getValue());
//            }

            httpUtils.send(HttpRequest.HttpMethod.POST, dataRequest.getIp()
                            + dataRequest.getActionName(), params,
                    new RequestCallBack<String>() {
                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            dataRequest.setNetError(false);
                            LogUtil.d("resp data:", responseInfo.result);
                            dataRequest.setResponseData(responseInfo.result);
                            dataRequest.getCallBack().onLoaded(
                                    dataRequest);
                        }

                        @Override
                        public void onFailure(HttpException e, String s) {
                            LogUtil.d("resp data:error ", e);
                            dataRequest.setNetError(true);
                            dataRequest.setResponseData(s);
                            dataRequest.getCallBack().onLoaded(
                                    dataRequest);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.d("req data err", e.getMessage());
            dataRequest.setNetError(true);
            dataRequest.getCallBack().onLoaded(dataRequest);
        }
    }

}
